<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var names = ["abc", "cba", "nba", "mba"]
    names.forEach(function (item) { console.log(item, this) }, { name: 'why' })

    // filter过滤
    var nums = [11, 20, 55, 100, 88, 32]
    console.log(nums.filter(item => item % 2 === 0)) // [20, 100, 88, 32]

    // map 映射
    console.log(nums.map(item => item * item)) // [121, 400, 3025, 10000, 7744, 1024]

    // reduce 累加
    console.log(nums.reduce((preValue, item) => preValue + item, 100000)) // 100306
    console.log(names.reduce((preValue, item) => preValue + "_" + item)) // abc_cba_nba_mba

    var products = [
      { name: "鼠标", price: 100, count: 3 },
      { name: "键盘", price: 200, count: 2 },
      { name: "耳机", price: 150, count: 2 },
    ]
    var totalPrice = products.reduce((preValue, item) => preValue + item.price * item.count, 0)
    console.log(totalPrice)

    var nums = [11, 20, 55, 100, 88, 32]
    // 过滤所有偶数 并计算他们的平方和
    console.log(
      nums
        .filter(item => item % 2 === 0)
        .map(item => item * item)
        .reduce((preValue, item) => preValue + item, 0)
    )

  </script>
</body>

</html>